
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test2 {
    public static void main(String[] args)  {
        Class<?> clazz = null;
        try {
            clazz = Class.forName("Techer");
            Constructor<?> constructor = clazz.getConstructor();
            Techer t = (Techer) constructor.newInstance();
            Method speakMethod = clazz.getMethod("speak",String.class);
            speakMethod.invoke(t,"haha");
            Field positionField = clazz.getField("positon");
            positionField.set(t, "Master");
            Field salaryField = clazz.getDeclaredField("salary");
            salaryField.setAccessible(true); // 设置私有属性为可访问
            salaryField.set(t, 7000);
            System.out.println(t);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }
}

